home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / xceedzip / MEMORY C.CPP < prev    next >
C/C++ Source or Header  |  1999-04-26  |  5KB  |  172 lines

  1. // Memory Compression.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "zipDispIds.h" // DISPIDs for methods, properties and events
  6.  
  7. // The following line imports the type library from XceedZip.dll, and creates
  8. // wrapper classes for the controls (in XceedZip.tlh and XceedZip.tli).
  9.  
  10. #import "XceedZip.dll" no_namespace named_guids
  11.  
  12. // Change these defines to customize the sample
  13.  
  14. #define FILE_TO_ZIP         _T( "c:\\test.txt" )
  15. #define ZIP_FILENAME        _T( "c:\\test.zip" )
  16.  
  17. // This identifies the our sink object (it could be any id...)
  18.  
  19. #define DISPID_ZIP_SINK     1
  20.  
  21. // Usually, a sink object derives from IDispEventImpl, which uses type info from the
  22. // event source's typelib to get the funcinfo. But IDispEventImpl::GetUserDefinedType()
  23. // only handles TKIND_ALIAS. In many of our events, we have TKIND_ENUM parameters.
  24. // This is why the sink object derives from IDispEventSimpleImpl, which does not use
  25. // type information. We need to provide a _ATL_FUNC_INFO for each event we want to
  26. // handle.
  27.  
  28. static _ATL_FUNC_INFO QueryMemoryFile_Info = 
  29. {
  30.    CC_STDCALL,   // Calling convention.
  31.    VT_EMPTY,     // Return type.
  32.    10,           // Number of arguments.
  33.    { VT_I4 | VT_BYREF, VT_BSTR | VT_BYREF, VT_BSTR | VT_BYREF, VT_I4 | VT_BYREF,
  34.      VT_DATE | VT_BYREF, VT_DATE | VT_BYREF, VT_DATE | VT_BYREF, 
  35.      VT_BOOL | VT_BYREF, VT_BSTR | VT_BYREF, VT_BOOL | VT_BYREF } // Argument types
  36. };
  37.  
  38. static _ATL_FUNC_INFO ZippingMemoryFile_Info = 
  39. {
  40.    CC_STDCALL,   // Calling convention.
  41.    VT_EMPTY,     // Return type.
  42.    4,            // Number of arguments.
  43.    { VT_I4, VT_BSTR, VT_VARIANT | VT_BYREF, VT_BOOL | VT_BYREF } // Argument types
  44. };
  45.  
  46. // The class CZipEventSink is the event sink.
  47.  
  48. class CZipEventSink: public IDispEventSimpleImpl< DISPID_ZIP_SINK, CZipEventSink, &DIID__IXceedZipEvents >
  49. {
  50. public:
  51.   void _stdcall QueryMemoryFile (
  52.         long * lUserTag,
  53.         BSTR * sFilename,
  54.         BSTR * sComment,
  55.         long * lAttributes,
  56.         DATE * dtLastModified,
  57.         DATE * dtLastAccessed,
  58.         DATE * dtCreated,
  59.         VARIANT_BOOL * bEncrypted,
  60.         BSTR * sPassword,
  61.         VARIANT_BOOL * bFileProvided )
  62.   {
  63.     if( m_bMustProvideFile )
  64.     {
  65.       m_bMustProvideFile = false;
  66.  
  67.       // Must realloc the provided pointers!
  68.  
  69.       SysReAllocString( sFilename, L"test.dat" );
  70.       *bFileProvided = VARIANT_TRUE;
  71.     }
  72.     else
  73.     {
  74.       *bFileProvided = VARIANT_FALSE;
  75.     }
  76.   }
  77.  
  78.   void _stdcall ZippingMemoryFile (
  79.         long lUserTag,
  80.         BSTR sFilename,
  81.         VARIANT * vaDataToCompress,
  82.         VARIANT_BOOL * bEndOfData )
  83.   {
  84.     HANDLE hFile = CreateFile( FILE_TO_ZIP, GENERIC_READ, FILE_SHARE_READ, 
  85.                                NULL, OPEN_EXISTING, 0, NULL );
  86.  
  87.     if( hFile != INVALID_HANDLE_VALUE )
  88.     {
  89.       BY_HANDLE_FILE_INFORMATION xFileInfo = { 0 };
  90.       GetFileInformationByHandle( hFile, &xFileInfo );
  91.  
  92.       // We create a safearray that will contain the data to be zipped
  93.  
  94.       SAFEARRAY* psa = SafeArrayCreateVector( VT_I1, 0, xFileInfo.nFileSizeLow );
  95.  
  96.       BYTE* pcData;
  97.       DWORD dwBytesRead;
  98.  
  99.       SafeArrayAccessData( psa, ( void** )&pcData );
  100.  
  101.       ReadFile( hFile, pcData, xFileInfo.nFileSizeLow, &dwBytesRead, NULL );
  102.  
  103.       SafeArrayUnaccessData( psa );
  104.       CloseHandle( hFile );
  105.  
  106.       // We set the variant vaDataToCompress to the safearray we just created.
  107.  
  108.       vaDataToCompress->vt = VT_UI1 | VT_ARRAY;
  109.       vaDataToCompress->parray = psa;
  110.     }
  111.  
  112.     *bEndOfData = VARIANT_TRUE;
  113.   }
  114.  
  115. // The sink map is used by IDispEventSimpleImpl<>. The map contains information about
  116. // each event handler we want to provide.
  117.  
  118. BEGIN_SINK_MAP( CZipEventSink )   
  119.   SINK_ENTRY_INFO(DISPID_ZIP_SINK, DIID__IXceedZipEvents, XCD_ZIP_DISPID_QUERYMEMORYFILE, QueryMemoryFile, &QueryMemoryFile_Info)
  120.   SINK_ENTRY_INFO(DISPID_ZIP_SINK, DIID__IXceedZipEvents, XCD_ZIP_DISPID_ZIPPINGMEMORYFILE, ZippingMemoryFile, &ZippingMemoryFile_Info)
  121. END_SINK_MAP()
  122.  
  123.   CZipEventSink( void )
  124.   {
  125.     m_bMustProvideFile = true;
  126.   }
  127.  
  128. private:
  129.   bool m_bMustProvideFile;
  130. };
  131.  
  132. int main(int argc, char* argv[])
  133. {
  134.   CoInitialize( NULL );
  135.  
  136.   try 
  137.   {
  138.     IXceedZipPtr pZip( CLSID_XceedZip );    // Instanciates XceedZip
  139.  
  140.     CZipEventSink xEventSink;               // Instanciates the event sink
  141.    
  142.     xEventSink.DispEventAdvise( pZip );     // Connects the event sink to XceedZip
  143.  
  144.     DeleteFile( ZIP_FILENAME );
  145.  
  146.     pZip->ZipFilename = ZIP_FILENAME;
  147.  
  148.     pZip->Zip();
  149.  
  150.     xEventSink.DispEventUnadvise( pZip );
  151.   }
  152.   catch( const _com_error& xErr ) 
  153.   {
  154.     // The generated wrapper classes throw _com_error exceptions when a COM error
  155.     // occurs.
  156.  
  157.     printf( "\nCOM Error 0x%08X ( %s ).\n", xErr.Error(), xErr.ErrorMessage() );
  158.   }
  159.   catch ( ... ) 
  160.   {
  161.     printf( "\nUnhandled Exception.\n" );
  162.   }
  163.  
  164.   CoUninitialize();
  165.  
  166.   printf( "\nPress any key to quit...\n" );
  167.   getch();
  168.  
  169.     return 0;
  170. }
  171.  
  172.